<template>
  <div>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="分类名称" prop="cateName">
        <el-input v-model="form.cateName" />
      </el-form-item>

      <el-form-item label="上级分类" prop="pid">
        <el-cascader
          v-model="form.pid"
          :options="options"
          :props="{
            checkStrictly: true,
            emitPath: false,
            label: 'cateName',
            value: 'id',
          }"
          clearable
        />
      </el-form-item>

      <el-form-item label="分类图片">
        <div class="bis">
          <el-upload
            ref="uploadRef"
            action="api/upIoad"
            class="avatar-uploader"
            :show-file-list="false"
            name='file'
            accept=".jpg,.png,.gif"
            :data="{a:1,b:2}"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.icon" :src="form.icon" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </div>
      </el-form-item>

      <el-form-item label="状态" prop="enabl">
        <el-radio-group v-model="form.enabl">
          <el-radio :label="true">显示</el-radio>
          <el-radio :label="false">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="排序" prop="sort" >
        <el-input v-model="form.sort" type="Number" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">新增</el-button>
        <el-button type="primary" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { fatchAddCates } from '@a/cateLists'
export default {
  props: {
    cates: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      imageUrl: '',
      rules: {
        cateName: {
          required: true,
          message: '分类名称必填'
        },
        enabl: {
          required: true,
          message: '状态必选'
        },
        pid: {
          required: true,
          message: '上级分类必选'
        },
        sort: {
          required: true,
          message: '排序必选'
        }
      },
      form: {
        cateName: '',
        icon: '',
        enabl: true,
        pid: -1,
        sort: 1
      },
      options: [
        {
          id: -1,
          cateName: '顶层类目'
        }
      ]
    }
  },
  watch: {
    cates: {
      handler (newVal) {
        console.log(newVal)
        this.options = [...this.options, ...newVal]
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    // 图片上传
    handleAvatarSuccess (res) {
      if (res.code === 200) {
        this.form.icon = res.data.url
        this.$message({
          type: 'success',
          message: res.msg
        })
      }
      /* console.log('上传')
      console.log(res) */
    },
    // 图片上传之前
    beforeAvatarUpload (file) {
      // console.log(file)
      if (file.size > 1024 * 1024 * 5) {
        this.$message({
          type: 'error',
          message: '上传的图片不能超过1MB'
        })
        return false
      } else {
        return true
      }
    },

    onSubmit () {
      // console.log(this.form)
      this.$refs.ruleFormRef
        .validate()
        .then(() => {
          // 转换sort为数值
          this.form.sort = Number(this.form.sort)
          fatchAddCates(this.form).then((res) => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: res.data.msg,
                deration: 1500,
                onClose: () => {
                  this.$router.go(0)
                }
              })
            }
          })
        })
        .catch((res) => {
          this.$message({
            type: 'error',
            message: '验证失败',
            deration: 1500
          })
        })
    },
    onReset () {
      this.$refs.ruleFormRef.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.bis {
  border: 1px dashed #dcdfe6;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
